package com.sjb.cp.controller;

import com.sjb.cp.annotation.AuthCheck;
import com.sjb.cp.common.BaseResponse;
import com.sjb.cp.common.ResultUtils;
import com.sjb.cp.exception.ErrorCode;
import com.sjb.cp.exception.ThrowUtils;
import com.sjb.cp.manager.auth.SpaceUserAuthManager;
import com.sjb.cp.model.constant.UserConstant;
import com.sjb.cp.model.po.Space;
import com.sjb.cp.model.po.User;
import com.sjb.cp.model.request.space.SpaceAddRequest;
import com.sjb.cp.model.request.space.SpaceQueryRequest;
import com.sjb.cp.model.request.space.SpaceUpdateRequest;
import com.sjb.cp.model.vo.SpaceVO;
import com.sjb.cp.service.SpaceService;
import com.sjb.cp.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author:sjb
 * @CreateTime:2025-01-26
 * @Description: 空间控制视图
 * @Version：1.0
 */
@RestController
@RequestMapping("/space")
public class SpaceController {

    @Resource
    private SpaceService spaceService;

    @Resource
    private UserService userService;

    @Resource
    private SpaceUserAuthManager spaceUserAuthManager;

    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.MUST_ADMIN)
    public BaseResponse<Boolean> updateSpace(@RequestBody SpaceUpdateRequest spaceUpdateRequest,
                                             HttpServletRequest request) {
        ThrowUtils.throwIf(spaceUpdateRequest == null, ErrorCode.PARAMS_ERROR);
        Space space = new Space();
        // 填充属性
        BeanUtils.copyProperties(spaceUpdateRequest,space);
        spaceService.fillSpaceBySpaceLevel(space);
        // 数据校验
        spaceService.validateSpace(false,space);
        Long id = spaceUpdateRequest.getId();
        Space querySpace = spaceService.getById(id);
        ThrowUtils.throwIf(querySpace == null,ErrorCode.NOT_FOUND_ERROR);
        boolean updated = spaceService.updateById(space);
        ThrowUtils.throwIf(!updated, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    @PostMapping("/add")
    public BaseResponse<Long> addSpace(@RequestBody SpaceAddRequest spaceAddRequest,
                                          HttpServletRequest request){
        ThrowUtils.throwIf(spaceAddRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        long addedSpace = spaceService.addSpace(spaceAddRequest, loginUser);
        return ResultUtils.success(addedSpace);
    }

    @PostMapping("/getById/space")
    public BaseResponse<SpaceVO> addSpace(@RequestBody SpaceQueryRequest spaceQueryRequest,
                                          HttpServletRequest request){
        ThrowUtils.throwIf(spaceQueryRequest == null, ErrorCode.PARAMS_ERROR);
        User loginUser = userService.getLoginUser(request);
        Space space = spaceService.getById(spaceQueryRequest.getId());
        SpaceVO spaceVo = spaceService.getSpaceVo(space, request);
        List<String> permissionsList = spaceUserAuthManager.getPermissionsList(space, loginUser);
        spaceVo.setPermissions(permissionsList);
        return ResultUtils.success(spaceVo);
    }
}
